import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:power_distribution_system/common/system_config.dart';
import 'package:power_distribution_system/utils/tools/screenutil_tool.dart';
import 'package:power_distribution_system/utils/widget/toast_widget.dart';


class TextfieldWidget extends StatelessWidget {
  String _title;
  int _code;
  static List<TextEditingController> _ctrl = [];
  static List<FocusNode> _node = [];


  // 旧密码
  TextEditingController oldPassword = TextEditingController();
  FocusNode _oldPwdFocus = FocusNode();

  // 新密码
  TextEditingController newPassword = TextEditingController();
  FocusNode _newPwdFocus = FocusNode();

  // 密码确认
  TextEditingController contrastPassword = TextEditingController();
  FocusNode _contrastPwdFocus = FocusNode();


  TextfieldWidget(String title, int code) {
    _title = title;
    _code = code;
    _ctrl = [ oldPassword, newPassword, contrastPassword ];
    _node = [_oldPwdFocus, _newPwdFocus, _contrastPwdFocus];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: ST.setw(32)),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: SystemConfig.borderColor,
            width: ST.setw(1)
          )
        )
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: ST.setw(170),
            child: Text("$_title：",
              textAlign: TextAlign.right,
              style: TextStyle(fontSize: SystemConfig.fs_content3,)
            )
          ),
          Expanded(
            child: TextField(
              controller: _ctrl[_code],
              focusNode: _node[_code],
              keyboardType: TextInputType.visiblePassword,
              textInputAction: TextInputAction.done,
              keyboardAppearance: Brightness.light,
              obscureText: true,
              decoration: InputDecoration(
                hintText: "${ _code == 2 ? "请再次输入新密码" : "请输入$_title" }",
                hintStyle: TextStyle(fontSize: SystemConfig.fs_title3),
                border: InputBorder.none,
              ),
            ),
          )
        ],
      ),
    );
  }

  /// ### 验证输入的内容
  static Map _verificaNewPwdInfo() {
    String msg;
    // 验证老密码
    if(_ctrl[0].text == null || _ctrl[0].text.length <= 5) {
      _ctrl[0].text = "";
      return { "state": false, "msg" : "旧密码输入有误"  };
    }

    if(_ctrl[1].text == null || _ctrl[1].text.length <= 5) {
      _ctrl[1].text = "";
      return { "state": false, "msg" : "新密码输入有误" };
    }

    if (_ctrl[2].text == null || _ctrl[1].text != _ctrl[2].text) {
      _ctrl[2].text = "";
      return { "state": false, "msg" : "两次密码输入不一致" };
    }

    return { "state": true,  "msg" : msg,
      "data": { "password": _ctrl[0].text, "nowPassword": _ctrl[1].text }
    };
  }

  /// ### 获取内容结果
  /// #### [context] BuildContext|必填|上下文
  static Map getResult(BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());

    // 输入验证
    Map result = _verificaNewPwdInfo();

    if(result["state"]) {
      // 剔除确认密码项
      result["data"].remove("nowPasswords");
      return result["data"];
    } else {
      ToastWidget.toast(result["msg"]);
    }
  }

}